package com.beerandjesus.calc.parse;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Date: Feb 20, 2008
 * Time: 6:45:33 PM
 */
public class TrackAndTime {
    public enum InputFormat {
        ENDS_WITH_TIME
    }

    private String track;

    private Calendar time = Calendar.getInstance();
    private SimpleDateFormat format = new SimpleDateFormat("H'h' m:ss");


    public TrackAndTime(String input, InputFormat inputFormat) {
        switch (inputFormat) {
            case ENDS_WITH_TIME:
                parseEndsWithTime(input);
        }
    }

    public String getTrack() {
        return track;
    }

    public void setTrack(String track) {
        this.track = track;
    }

    public Calendar getTime() {
        return time;
    }

    public void setTime(Calendar time) {
        this.time = time;
    }

    private void parseEndsWithTime(String input) {
        String title = input.trim();
        int colon = title.lastIndexOf(":");
        int tabBeforeTime = title.lastIndexOf("\t", colon);
        int spaceBeforeTime = title.lastIndexOf(" ", colon);
        if (tabBeforeTime > spaceBeforeTime) {
            spaceBeforeTime = tabBeforeTime;
        }
        String trackString = title.substring(0, spaceBeforeTime);
        track = cleanse(trackString);
        String timeString = title.substring(spaceBeforeTime).trim();
        String[] parts = timeString.split(":");
        time.clear();
        time.set(Calendar.HOUR, 0);
        if ((parts[0] != null) && !parts[0].equals("")) {
            time.set(Calendar.MINUTE, new Integer(parts[0]));
        }
        time.set(Calendar.SECOND, new Integer(parts[1]));
    }

    private String cleanse(String aString) {
        String newString = aString;
        for (int index = 1; index <= newString.length(); index++) {
            String character = newString.substring(index - 1, index);
            character.codePointAt(0);
        }
        newString = newString.replaceAll("-", "");
        newString = newString.replaceAll("�", "");
        newString = newString.replaceAll("\\?", "");
        newString = newString.replaceAll("!", "");
        newString = newString.replaceAll("#", "");
        newString = newString.replaceAll(":", "");
        newString = newString.replaceAll("\\.", "");
        newString = newString.replaceAll("\\$", "");
        newString = newString.replaceAll("\"", "");
        newString = newString.replaceAll("\t", " ");
        while (newString.contains("  ")) {
            newString = newString.replaceAll("  ", " ");
        }
        return newString.trim();
    }

    public String getTimeString() {
        return format.format(time.getTime());
    }

    public String getTotalBreakdown() {
        StringBuffer sb = new StringBuffer();
        int codePoint = "�".codePointAt(0);
        sb.append("� = " + codePoint + "\n");
        for (int index = 1; index <= track.length(); index++) {
            String character = track.substring(index - 1, index);
            sb.append(character + " = " + character.codePointAt(0) + "\n");
            character.codePointCount(0,0);
        }
        return sb.toString();
    }
}
